import { fileURLToPath, URL } from 'node:url'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import { defineConfig, loadEnv, ConfigEnv } from 'vite'
// import { visualizer } from 'rollup-plugin-visualizer'
import viteCompression from 'vite-plugin-compression'
import { createHtmlPlugin } from 'vite-plugin-html'

// https://vitejs.dev/config/
export default defineConfig((mode: ConfigEnv) => {
  const env = loadEnv(mode.mode, process.cwd())
  return {
    plugins: [
      vue(),
      vueJsx(),
      // visualizer({
      //   open: true, //在默认用户代理中打开生成的文件
      //   gzipSize: true, // 是否统计并显示gzip
      //   brotliSize: true // 是否统计并显示brotli
      // }),
      viteCompression({
        verbose: true, // 是否在控制台中输出压缩结果
        disable: false,
        threshold: 10240, // 如果体积大于阈值，将被压缩，单位为b，体积过小时请不要压缩，以免适得其反
        algorithm: 'gzip', // 压缩算法，可选['gzip'，' brotliccompress '，'deflate '，'deflateRaw']
        ext: '.gz',
        deleteOriginFile: true // 源文件压缩后是否删除(我为了看压缩后的效果，先选择了true)
      }),
      createHtmlPlugin({
        template: 'index.html',
        inject: {
          data: {
            title: '我的Vite项目'
          }
        }
      })
    ],
    root: process.cwd(),
    resolve: {
      alias: {
        '@': fileURLToPath(new URL('./src', import.meta.url))
      }
    },
    base: mode.command === 'serve' ? './' : env.VITE_PUBLIC_PATH,
    esbuild: {
      drop: ['debugger'] // 删除 debugger
    },
    build: {
      outDir: 'dist',
      chunkSizeWarningLimit: 1500,

      rollupOptions: {
        output: {
          chunkFileNames: 'assets/js/[name]-[hash].js',
          entryFileNames: 'assets/js/[name]-[hash].js',
          assetFileNames: 'assets/[ext]/[name]-[hash].[ext]',
          manualChunks(id) {
            if (id.includes('node_modules')) {
              return (
                id.toString().match(/\/node_modules\/(?!.pnpm)(?<moduleName>[^\/]*)\//)?.groups!
                  .moduleName ?? 'vender'
              )
            }
          }
        }
      }
    },
    css: {
      preprocessorOptions: {
        scss: {
          javascriptEnabled: true,
          additionalData: `@import "@/style/settings/var.scss";
          @import "@/style/tools/_sassMagic.scss";`
        }
      }
    },

    server: {
      host: '0.0.0.0',
      port: env.VITE_PORT as unknown as number,
      open: JSON.parse(env.VITE_OPEN),
      hmr: true,
      proxy: {
        ['/api']: {
          target: 'http://localhost:7006',
          //target: 'http://111.229.109.174:7006',
          changeOrigin: true
        },
        '/auth': {
          target: 'http://localhost:7006',
          changeOrigin: true
        },
        '/folder-test': {
          target: 'http://localhost:7006',
          changeOrigin: true
        }
      }
    },
    define: {
      __NEXT_NAME__: JSON.stringify(process.env.npm_package_name)
    }
  }
})

// export default defineConfig({
//   plugins: [vue(), vueJsx()],
//   resolve: {
//     alias: {
//       '@': fileURLToPath(new URL('./src', import.meta.url))
//     }
//   },
//   css: {
//     preprocessorOptions: {
//       scss: {
//         javascriptEnabled: true,
//         additionalData: `@import "@/style/settings/var.scss";
//         @import "@/style/tools/_sassMagic.scss";`
//       }
//     }
//   },
//   server: {
//     host: '0.0.0.0',
//     port: env.VITE_PORT as unknown as number,
//     open: JSON.parse(env.VITE_OPEN),
//     hmr: true,
//     proxy: {
//       ['/api']: {
//         target: 'http://localhost:7006',
//         //target: 'http://111.229.109.174:7006',
//         changeOrigin: true
//       },
//       '/auth': {
//         target: 'http://localhost:7006',
//         changeOrigin: true
//       },
//       '/folder-test': {
//         target: 'http://localhost:7006',
//         changeOrigin: true
//       }
//     }
//   },
//   define: {
//     __NEXT_NAME__: JSON.stringify(process.env.npm_package_name)
//   }
// })
